<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\Card;
use plugin\admin\app\model\ExploreAddress;
use plugin\admin\app\model\ExploreTaskReward;
use plugin\admin\app\model\Material;
use plugin\admin\app\model\Ornament;
use plugin\admin\app\model\Skin;
use plugin\admin\app\model\Staff;
use plugin\admin\app\model\StaffFr;
use support\exception\BusinessException;
use support\Request;
use support\Response;

/**
 * 探索任务奖励表
 */
class ExploreTaskRewardController extends Crud
{

    /**
     * @var ExploreTaskReward
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new ExploreTaskReward;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('explore-task-reward/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('explore-task-reward/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('explore-task-reward/update');
    }

    /**
     * 树状任务
     * @param Request $request
     * @return Response
     */
    public function task_tree(Request $request): Response
    {
        $address_list = ExploreAddress::with([
            'chapter' => function ($query) {
                $query->select('id', 'cn_name', 'explore_address_id'); // 限制故事的字段，必须包含外键
            },
            'chapter.story' => function ($query) {
                $query->select('id', 'cn_name', 'explore_chapter_id'); // 限制任务的字段，必须包含外键
            },
            'chapter.story.task' => function ($query) {
                $query->select('id', 'cn_name', 'explore_story_id'); // 限制任务的字段，必须包含外键
            }
        ])->select('id', 'cn_name')->orderBy('id', 'desc')->get()->toArray();
        $data = [];
        foreach ($address_list as $address) {
            $item = [
                'id' => $address['id'],
                'name' => $address['cn_name'],
                'pid' => null,
                'value' => $address['id'],
                'children' => []
            ];
            foreach ($address['chapter'] as $chapter_key => $chapter) {
                $item['children'][$chapter_key] = [
                    'id' => $chapter['id'],
                    'name' => $chapter['cn_name'],
                    'pid' => $address['id'],
                    'value' => $chapter['id'],
                    'children' => []
                ];
                foreach ($chapter['story'] as $story_key => $story) {
                    $item['children'][$chapter_key]['children'][$story_key] = [
                        'id' => $story['id'],
                        'name' => $story['cn_name'],
                        'pid' => $chapter['id'],
                        'value' => $story['id'],
                        'children' => []
                    ];
                    foreach ($story['task'] as $task_key => $task) {
                        $item['children'][$chapter_key]['children'][$story_key]['children'][$task_key] = [
                            'id' => $task['id'],
                            'name' => $address['cn_name'] . '-' . $chapter['cn_name'] . '-' . $story['cn_name'] . '-' . $task['cn_name'],
                            'pid' => $story['id'],
                            'value' => $task['id'],
                            'children' => []
                        ];
                    }
                }
            }
            $data[] = $item;
        }

        return json(['code' => 0, 'data' => $data, 'msg' => 'ok']);
    }

    //奖励列表
    public function reward_tree(Request $request): Response
    {
        //材料
        $list['材料'] = Material::select('id', 'cn_name')->get()->toArray();
        //角色皮肤
        $list['角色皮肤'] = Skin::select('id', 'cn_name')->get()->toArray();
        //角色
        $list['角色'] = Staff::select('id', 'cn_name')->get()->toArray();
        //角色碎片
        $list['角色碎片'] = StaffFr::select('id', 'cn_name')->get()->toArray();
        //卡牌
        $list['卡牌'] = Card::select('id', 'cn_name')->get()->toArray();
        //建筑装饰品
        $list['建筑装饰品'] = Ornament::select('id', 'cn_name')->get()->toArray();

        $data = [];
        $index = 0;
        foreach ($list as $key => $item) {
            $index++;
            $res = [
                'id' => $index,
                'name' => $key,
                'pid' => null,
                'value' => $index,
                'children' => []
            ];
            foreach ($item as $key2 => $value2) {
                $res['children'][] = [
                    'id' => $value2['id'],
                    'name' => $value2['cn_name'],
                    'pid' => $index,
                    'value' => $value2['id'],
                    'children' => []
                ];
            }
            $data[] = $res;
        }


        return json(['code' => 0, 'data' => $data, 'msg' => 'ok']);

    }


}
